package com.baizhi.ems_thymeleaf.service.impl;

import com.baizhi.ems_thymeleaf.mapper.UserMapper;
import com.baizhi.ems_thymeleaf.pojo.User;
import com.baizhi.ems_thymeleaf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Override
    public void regist(User user) {
        //查詢用戶名是否存在
        List<User> list = userMapper.findUserByUsername(user.getUsername());
        if (list.size() > 0){
            throw new RuntimeException("該用戶已經存在");
        }
        userMapper.insert(user);
        //註冊
    }

    @Override
    public User login(User user) {
        User user1 = userMapper.findUserByUsernameAndPassword(user);
         if (user1 == null) throw  new RuntimeException("用戶名或密碼不正確");
        return user1;
    }
}
